package com.kuang.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuang.pojo.Product;
import com.kuang.pojo.Sale;
import com.kuang.service.ProductServiceImpl;
import com.kuang.service.SaleServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/sale")
public class SaleController {
    @Autowired
    private SaleServiceImpl saleService;
    @Autowired
    private ProductServiceImpl productService;
    //查询所有销售信息
@RequestMapping("/getSale")
public String  getSale(@RequestParam(required=true,value="pageNum",defaultValue="1") Integer pageNum, @RequestParam(required=true,value="pageSize",defaultValue="6")Integer pageSize, Model model){
    PageHelper.startPage(pageNum,pageSize);
    List<Sale> sale = saleService.getSale();
    PageInfo<Sale> salepageInfo=new PageInfo<>(sale);
    int pages = salepageInfo.getPages();
    if (pageNum==0 ||pageNum>salepageInfo.getPages()){
        return "redirect:/sale/getSale";
    }
    model.addAttribute("salepageInfo",salepageInfo);
    return "sale";
}
    //跳转到增加页面
    @RequestMapping("/redirectadd")
    public String redirectaddSale(HttpServletRequest request,Model model){
        HttpSession session = request.getSession();
        session.removeAttribute("msgadd");
        List<Product> product = productService.getProduct();
//        //循环变量集合 当数量为0的时候  就移除数据
//       for (int i=0;i<product.size();i++){
//           if (Double.parseDouble(product.get(i).getNumber())<=0){
//               product.remove(i);
//           }
//       }
        model.addAttribute("product",product);
        return "addsale";
    }
    //数据输入错误的时候，添加添加按钮会跳转到这个方法
    @RequestMapping("/redirectadd04")
    public String redirectaddSale04(HttpServletRequest request,Model model){
        List<Product> product = productService.getProduct();
//        List<Sale> sale = saleService.getSale();
//        ListIterator<Product> productListIterator = product.listIterator();
//        ListIterator<Sale> saleListIterator = sale.listIterator();
       /* for (int i=0;i<product.size();i++){
            if (product.get(i).getId().equals(sale.get(i).getId())){
                product.remove(i);
            }

        }
        //循环变量集合 当数量为0的时候  就移除数据
        for (int i=0;i<product.size();i++){
            if (Double.parseDouble(product.get(i).getNumber())<=0){
                product.remove(i);
            }
        }
*/
        model.addAttribute("product",product);
        return "addsale";
    }

    //增加方法
    @RequestMapping("/addsale")
    public String addSale(HttpServletRequest request, Sale sale, Model model){
    //对销售表执行增加
        String name = sale.getName();//名称
        String number = sale.getNumber();//数量
        String date = sale.getDate();//时间
        String detail = sale.getDetail();//备注


        //根据商品名称获取商品id
       // String strings = productService.productidbyname(name);
        int productidbyname = productService.productidbyname(name);
        String s2 = Integer.toString(productidbyname);
        System.out.println("=============="+productidbyname+"=============");

        //库存模块开始：减去数量
        Product productById1 = productService.getProductById(productidbyname);
        double v = Double.parseDouble(productById1.getNumber());//原先的库存数量
        double v1 = Double.parseDouble(number);
        double v3=v-v1;
        if (v3<=-1){
            HttpSession session = request.getSession();
            session.setAttribute("msgadd","库存不足，请重新输入或者添加库存");
            return "redirect:/sale/redirectadd04";
        }
        //
        String s = Double.toString(v3);
        //重新设置库存数量
        productService.updateById(new Product(productidbyname,s));
        //库存模块结束


        //根据商品id获取获取商品表的  编号  价格
        Product productById = productService.getProductById(productidbyname);
        String number1 = productById.getNumber();


        //库存表根据名字获取数量
        //通过名字判断销售表中是否有商品
        List<Sale> sale1 = saleService.getSale();
        for (int i=0;i<sale1.size();i++){
            if (sale1.get(i).getName().equals(name)){
                Sale saleById = saleService.getSaleById(s2);
                String number2 = saleById.getNumber();
                double v2 = Double.parseDouble(number);
                double v5 = Double.parseDouble(number2);
                double v4=v2+v5;
                String s1 = Double.toString(v4);
                saleService.updateSale(new Sale(name,s1,date,detail));
                return "redirect:/sale/getSale";
            }

        }
       int id = productById.getId();
        String price = productById.getPrice();
        String suppiler = productById.getSuppiler();
        String tell="123456";
        saleService.addSale(new Sale(s2,name,price,number,date,tell,suppiler,detail));





        return "redirect:/sale/getSale";
    }
    //跳转到修改页面
    @RequestMapping("/redirectUpdate")
    public String redirectUpdate(HttpServletRequest request,String id,Model model){
        HttpSession session = request.getSession();

        session.removeAttribute("msgxiugai");

        Sale saleById = saleService.getSaleById(id);
        String number = saleById.getNumber();
        session.setAttribute("salenumber",number);
        model.addAttribute("saleById",saleById);
        return "updatesale";
    }
//    //根据id查询 跳到修改页面
//    @RequestMapping("/redirectUpdate02")
//    public String redirectUpdate02(String id,Model model){
//        Sale saleById = saleService.getSaleById(id);
//        model.addAttribute("saleById",saleById);
//        return "updatesale02";
//    }
//    //根据id查询
//    @RequestMapping("/redirectUpdate05")
//    public String redirectUpdate05(String id,Model model){
//        Sale saleById = saleService.getSaleById(id);
//        model.addAttribute("saleById",saleById);
//        return "/putong/updatesale02";
//    }
    //销售界面修改数据
    @RequestMapping(value = "/update")
    public String updateSale(HttpServletRequest request,Sale sale){
        HttpSession session = request.getSession();
        String id = sale.getId();//获取商品的id
        //输入的数量如果大于库存的数量  直接跳转
        String number2 = sale.getNumber();
        double v2 = Double.parseDouble(number2);
        String productbyname = productService.productbyname(sale.getName());
        double v3 = Double.parseDouble(productbyname);
        if (v2>v3){
            session.setAttribute("msgxiugai","修改的数量已经超过库存，请重新输入或者修改库存数量");
            return "redirect:/sale/redirectUpdateerror?id="+id;
        }

        System.out.println(sale);


        int id02 = Integer.parseInt(id);
        //获取原先的数量
        String salenumber1 = (String)session.getAttribute("salenumber");
        double salenuber1 = Double.parseDouble(salenumber1);
        System.out.println(salenuber1);//输出商品原先的数量

        String number1 = sale.getNumber();//修改后数量
        // 修改后的数量  进行类型转化
        double  salenumber= Double.parseDouble(number1);

        Product productById = productService.getProductById(id02);//根据id 查找商品表信息
        String number = productById.getNumber();//获取数据库中商品的数量
        double v = Double.parseDouble(number);

        if (salenuber1>salenumber){//原先的数量大于修改的数量  是减去了数量  让库存增加
            double sub=salenuber1-salenumber;


            double vadd=sub+v;
            String v1 = Double.toString(vadd);
            productService.updateById(new Product(id02,v1));
        }else {  // 修改的数量大于原先的数量  的增加了值 让库存减少
             double sub02=salenumber-salenuber1;
             if (sub02>v){
                 session.setAttribute("msgxiugai","修改的数量已经超过库存，请重新输入或者修改库存数量");
                 return "redirect:/sale/redirectUpdateerror?id="+id;
             }else {
                 double sub03=v-sub02;
                 String s = Double.toString(sub03);
                 productService.updateById(new Product(id02,s));
             }
        }


        saleService.updateSale(sale);
        return "redirect:/sale/getSale";
    }
    // 修改发生错误后  重定向到这个方法
    @RequestMapping("/redirectUpdateerror")
    public String redirectUpdateerror(HttpServletRequest request,  String id, Model model){


        Sale saleById = saleService.getSaleById(id);
        model.addAttribute("saleById",saleById);
        return "updatesale";
    }
    //删除
    @RequestMapping("/delete")
    public String deleteSale(String id){
    saleService.deleteSale(id);
        return "redirect:/sale/getSale";
    }
    //根据名称查询销售表信息
    @RequestMapping("/getSqlByname")
 public  String getSqlByname(String name,Model model){
     List<Sale> sqlByname = saleService.getSqlByname(name);
      model.addAttribute("saleByName",sqlByname);
      return "bynamesale";
 }
    //根据id查询 显示在全页面上
    @RequestMapping("/getsaleById")
    public String redirectUpdate(String id,Model model){
        Sale list= saleService.getSaleById(id);

        model.addAttribute("list",list);
        return "byidsale";
    }
}

